using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace pxt
{
    public class CollisionSystem : Microsoft.Xna.Framework.GameComponent
    {
        // keep track of all projectiles
        protected List<Projectile> mProjectiles;
        protected List<Player> mPlayers;

        public CollisionSystem(Game game) : base(game)
        {
            // TODO: Construct any child components here
        }

        public override void Initialize()
        {
            mProjectiles = new List<Projectile>();
            mPlayers = new List<Player>();
            base.Initialize();
        }

        public override void Update(GameTime gt)
        {
            // TODO: Add your update code here
            for (int i = 0; i < mProjectiles.Count; i++)
            {
                mProjectiles[i].Update(gt);
                for (int j = 0; j < mPlayers.Count; j++)
                {
                    if (mProjectiles[i].ProjectileType == projectile_type.Revive)
                    {
                        if (mPlayers[j].Team == mProjectiles[i].Owner.Team)
                        {
                            if (mPlayers[j].Wizard.Alive == false)
                            {
                                mPlayers[j].Wizard.Alive = true;
                                mProjectiles[i].Kill();
                            }
                        }
                    }

                    if (mProjectiles[i].Bounds.Intersects(mPlayers[j].Wizard.Bounds))
                    {
                        // A projectile has collided with a player, lets do more tests!
                        if (mPlayers[j] != mProjectiles[i].Owner && mProjectiles[i].Active && mPlayers[j].Wizard.Alive)
                        {
                            // reflect if we have too!
                            if (mPlayers[j].Wizard.Reflect)
                            {
                                mProjectiles[i].Owner.Wizard.TakeDamage(mProjectiles[i].Damage);
                            }
                            else // do dmg to the target
                            {
                                mPlayers[j].Wizard.TakeDamage(mProjectiles[i].Damage);
                                if (!mPlayers[j].Wizard.Alive)
                                {
                                    mProjectiles[i].Owner.Stats.Kills++;
                                    mPlayers[j].Stats.Deaths++;
                                }
                            }

                            mProjectiles[i].Kill();
                        }
                    }
                }
            }
            for (int i = 0; i < mProjectiles.Count; i++)
            {
                if (!mProjectiles[i].Active)
                {
                    mProjectiles.RemoveAt(i);
                }
            }
            base.Update(gt);
        }

        public void Draw()
        {
            foreach (Projectile p in mProjectiles)
            {
                p.Draw();
            }
        }

        public void AddProjectile(Projectile p)
        {
            mProjectiles.Add(p);
        }

        public void AddPlayer(Player p)
        {
            mPlayers.Add(p);
        }

        // Properties
        public List<Player> Players
        {
            get { return mPlayers; }
        }
    }
}